package com.tiemei.demo.unittest.mockito;

import static org.mockito.Mockito.*;

import java.util.LinkedList;
import java.util.List;

import org.junit.Test;

/**
 * <ul>
 * <li>times(int)</li>
 * <li>never()</li>
 * <li>atLeastOnce()</li>
 * <li>atLeast(int)</li>
 * <li>atMost(int)</li>
 * </ul>
 *
 * @author tiemei.jdk
 * @date   2012-10-15
 */
public class VerifyMethodInvokedTimes {
	@Test
	public void testInvokedTimes () {
		// 1. mock
		List mockedList = mock(LinkedList.class);
		
		// 2. run
		mockedList.add("once");
		
		mockedList.add("twice");  
		mockedList.add("twice");  
		
		mockedList.add("three times");  
		mockedList.add("three times");  
		mockedList.add("three times");  
		
		// 3. verify
		verify(mockedList).add("once");  
		verify(mockedList, times(1)).add("once");  
		
		verify(mockedList, times(2)).add("twice");  
		
		verify(mockedList, times(3)).add("three times");  
		
		verify(mockedList, atLeastOnce()).add("three times");  
		verify(mockedList, atLeast(2)).add("three times");  
		verify(mockedList, atMost(5)).add("three times");  
	}
	
	@Test
	public void testNeverInvoked () {
		// 1. mock
		List neverInvokedObj = mock(List.class);
		List neverInvokedObj2 = mock(List.class);
		
		// 2. run
		
		// 3. verify
		verifyZeroInteractions(neverInvokedObj, neverInvokedObj2);
		verify(neverInvokedObj, never()).add("add one");
	}
	
	@Test
	public void testNoExtraInvoked () {
		// 1. mock
		List mockedList = mock(LinkedList.class);
		
		// 2. run
		mockedList.add("one");  
		mockedList.add("two");  
		
		// 3. verify 
		// 验证除了add("one")没有其他的调用
		verify(mockedList).add("one"); 
		verifyNoMoreInteractions(mockedList);  
	}
}
